<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'bug_api.php' );


auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'MetadataStandards.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'ProjectsMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'HTTPclient.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableOutputs.php' );

/*
 Turn this on to update the metadata standards
 
$MetadataStandards = new MetadataStandards;
$MetadataStandards->saveMetadata();
$MetadataStandards->datasetMetadata();
*/



$authHashPrefix = plugin_config_get( 'authHashPrefix' );
$refineHumanURI = plugin_config_get( 'refineBaseURI' ); //for human linking
$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}

# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );






if(!isset($_GET["grProject"])){
	$GRprojectID =  false;
}
else{
	$GRprojectID = $_GET["grProject"]; 
}



$tableMetaObj = new TableMetadata;
$tableMetaObj->refineURI = $refineURI;
$tableMetaObj->refineHumanURI = $refineHumanURI;
$tableMetaObj->authHashPrefix = $authHashPrefix;
$tableMetaObj->currentUserID = $f_user_id;
$tableMetaObj->currentUserName = $u_username;
$tableMetaObj->GRprojectID = $GRprojectID;
$tableMetaObj->get_metadata();

$tableMetaObj->count_description_done(); //count number of completed general descriptive metadata fields;
$tableMetaObj = check_edit_resolution($tableMetaObj); //now check on the status of the edit issue
$tableMetaObj = check_metadata_fieldDes_resolution($tableMetaObj); //now check on status of the metadata desription and field documentation issues
$tableMetaObj = check_otherIssues_resolution($tableMetaObj); //finally check on the status of the "other issues"

$tableMetaObj->count_description_done();
$tableMetaObj->count_column_documentation();
$tableMetaObj->count_otherIssue_done(); //count other issues, and number resolved
$tableMetadata = $tableMetaObj->currentMetadata;

if(!check_dataset_projectID_auth($tableMetadata)){
	break; //not authorized to view, stop showing anything else!
}



html_page_top( "Data Documentation: Dataset Issue Tracking Summary" );

primary_links($tableMetadata, " (RESOLVED)"); //make HTML for primary links
other_links($tableMetadata, "(none)"); //make HTML links to other issues

$maxGraph = 500;
$propRequiredFieldsDone = $tableMetaObj->descriptionsDone / $tableMetaObj->numberRequiredDescript;
$pixelFieldsDone = round($propRequiredFieldsDone * $maxGraph, 0);

if($tableMetaObj->countNumOtherIssues > 0){
	$propOtherIssuesDone = $tableMetaObj->countNumOtherIssuesDone / $tableMetaObj->countNumOtherIssues;
	$pixelOtherIssues = round($propOtherIssuesDone * $maxGraph, 0);
}


$graphStyle = "margin-left:5px; height: 26px; text-align:center; padding-top:12px; background-color: #DCDCDC; ";
$fieldDoneStyle = $graphStyle."width: ".$pixelFieldsDone."px;";
$otherIssuesStyle = $graphStyle."width: ".$pixelOtherIssues."px;";
$resolvedIssueStyle = $graphStyle."width: ".$maxGraph."px;";
$openIssueStyle = $graphStyle."width: 1px;";

if(!isset($tableMetadata["columns"]) || !$tableMetadata["issueIDs"]["editResolved"]){
	$doneCols = false;
	$totalCols = "[None Ready for Documentation]";
	$columnDoneStyle = $graphStyle."width: 1px;";
}
else{
	$tableMetaObj->count_column_documentation();
	$tableMetadata = $tableMetaObj->currentMetadata;
	$totalCols = $tableMetaObj->numberColumns;
	$doneCols = $tableMetaObj->columnsDocumented;
	
	if($totalCols != 0){
		$pixelColsDone = round(($doneCols / $totalCols) * $maxGraph, 0);
	}
	else{
		$pixelColsDone = 1;
		$doneCols = false;
	}
	$columnDoneStyle = $graphStyle."width: ".$pixelColsDone."px;";
}



?>

<div class="data-doc-page-body">
	
	<!-- page heading information -->
	<div class="data-doc-page-heading">
		<div class="data-doc-page-heading-row">
			<div class="data-doc-page-heading-title">
				<h2>Dataset Issue Tracking Summary</h2>
				<br/>
				<br/>
				<span style="font-size:smaller;">Attempt Entity Reconciliation: [<a href="<?php echo "../plugin.php?page=DataRefine/data-doc-reconcile&grProject=".$GRprojectID; ?>" title="Relate this dataset to other data on the Web">Link these Data</a>]</span>
				<br/><span style="font-size:smaller;">Tips and How-to's on linking data in Google Refine [<a href="../plugin.php?page=DataRefine/data-doc-recipes">Useful Tips</a>]</span>
			</div>
			<div class="data-doc-page-heading-dset">
				<div class="data-doc-page-heading-dset-tab">
					<div class="data-doc-page-heading-dset-row">
						<div class="data-doc-page-heading-dset-title">
							<?php
							if(isset($tableMetadata["description"][0]["values"][0]["text"])){
								echo "<h3>&ldquo;".$tableMetadata["description"][0]["values"][0]["text"]."&rdquo;</h3>";
								echo "<br/><br/><span class=\"dest-sub-title\">(In refine as: <em>".$tableMetadata["name"]."</em>)</span>";	
							}
							else{
								echo "<h3>&ldquo;".$tableMetadata["name"]."&rdquo;</h3>";
							}
							
							?>
							
						</div>
						<div class="data-doc-page-heading-dset-des">
						This table currently has <span id="dataset-row-count"><?php echo $tableMetadata["refineRowCount"];  ?></span> rows. The last edit / modification of this table in Google Refine took place on <span id="dataset-last-modified"><?php echo date("F j, Y, g:i a", strtotime($tableMetadata["refineModified"]));  ?></span>. View the dataset in Google refine: [<a href="<?php echo $refineHumanURI."/project?project=".$GRprojectID;  ?>">Here</a>]
						<br/><br/>
						Download dataset metadata and documentation: [<a href="<?php echo "/plugins/DataRefine/metadata/".$GRprojectID.".json";  ?>">JSON-format</a>]
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- Main content for this dataset tracking page -->	
	<div class="data-doc-page-primary">
		<h4>Progress toward completing edits, resolving issues, and completing documentation</h4>
		<div style="display:table;">
			<div class="sum-div-row">
				<div class="sum-div-cell-title">
					<span class="step-title">Dataset Editing and Clean-up:</span>
					<br/>[<?php echo $editLink; ?>]
				</div>
				<div class="sum-div-cell-mid">
					<?php
					if(strlen($editResolved)>0){
						echo $editResolved;
					}
					else{
						echo "Needs Resolution";
					}
					?>
				</div>
				<div class="sum-div-cell-bar">
					<?php
					if(strlen($editResolved)>0){
						echo "<div style=\"".$resolvedIssueStyle."\">Done</div>";
					}
					else{
						echo "<div style=\"".$openIssueStyle."\">Open</div>";
					}
					?>
				</div>
			</div>
			<?php
			if($tableMetaObj->countNumOtherIssues > 0){
			?>
			
			<div class="sum-div-row">
				<div class="sum-div-cell-title">
					<span class="step-title">Other Issues:</span>
					<br/><?php echo $otherIssues; ?>
				</div>
				<div class="sum-div-cell-mid">
					<?php
					echo "(".$tableMetaObj->countNumOtherIssuesDone." of ".$tableMetaObj->countNumOtherIssues." resolved)";
					?>
				</div>
				<div class="sum-div-cell-bar">
					<?php
					echo "<div style=\"".$otherIssuesStyle."\">".(round($propOtherIssuesDone,1)*100)."%</div>";
					?>
				</div>
			</div>
			
			<?php
			}//end case with "other issues"
			?>
			<div class="sum-div-row">
				<div class="sum-div-cell-title">
					<span class="step-title"><a href="<?php echo "../plugin.php?page=DataRefine/data-doc-metadata&grProject=".$GRprojectID; ?>">General Descriptive Metadata</a>
					</span>
				</div>
				<div class="sum-div-cell-mid">
					<?php
					echo "(".$tableMetaObj->descriptionsDone." of ".$tableMetaObj->numberRequiredDescript." req. fields done)";
					?>
				</div>
				<div class="sum-div-cell-bar">
					<?php
					echo "<div style=\"".$fieldDoneStyle."\">".(round($propRequiredFieldsDone,1)*100)."%</div>";
					?>
				</div>
			</div>
			<div class="sum-div-row">
				<div class="sum-div-cell-title">
					<span class="step-title">Descriptive Metadata Sign-off:</span>
					<br/>[<?php echo $describeLink; ?>]
				</div>
				<div class="sum-div-cell-mid">
					<?php
					if(strlen($describeResolved)>0){
						echo $describeResolved;
					}
					else{
						echo "Needs Resolution";
					}
					?>
				</div>
				<div class="sum-div-cell-bar">
					<?php
					if(strlen($describeResolved)>0){
						echo "<div style=\"".$resolvedIssueStyle."\">Done</div>";
					}
					else{
						echo "<div style=\"".$openIssueStyle."\">Open</div>";
					}
					?>
				</div>
			</div>
			<div class="sum-div-row">
				<div class="sum-div-cell-title">
					<span class="step-title"><a href="<?php echo "../plugin.php?page=DataRefine/data-doc-fields&grProject=".$GRprojectID; ?>">Column Documentation:</a></span>
				</div>
				<div class="sum-div-cell-mid">
					<?php
					if(!$doneCols){
						echo "(Unknown)";
					}
					else{
						echo "(".$doneCols." of ".$totalCols." columns documented)";
					}
					?>
				</div>
				<div class="sum-div-cell-bar">
					<?php
					if(!$doneCols){
						echo "<div style=\"".$columnDoneStyle."\"></div>";
					}
					else{
						echo "<div style=\"".$columnDoneStyle."\">".(round(($doneCols / $totalCols),1)*100)."%</div>";	
					}
					?>
				</div>
			</div>
			<div class="sum-div-row">
				<div class="sum-div-cell-title">
					<span class="step-title">Column Documentation Sign-off:</span>
					<br/>[<?php echo $fieldsLink; ?>]
				</div>
				<div class="sum-div-cell-mid">
					<?php
					if(strlen($fieldsResolved)>0){
						echo $fieldsResolved;
					}
					else{
						echo "Needs Resolution";
					}
					?>
				</div>
				<div class="sum-div-cell-bar">
					<?php
					if(strlen($fieldsResolved)>0){
						echo "<div style=\"".$resolvedIssueStyle."\">Done</div>";
					}
					else{
						echo "<div style=\"".$openIssueStyle."\">Open</div>";
					}
					?>
				</div>
			</div>
		</div>
	</div>
	
	
</div>

	

<?php
html_page_bottom();
?>
